/*
 * Tencent is pleased to support the open source community by making ovCompose available.
 * Copyright (C) 2025 THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tencent.compose.sample

import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.napi.asString
import androidx.compose.ui.napi.js
import androidx.compose.ui.unit.dp
import kotlinx.cinterop.*
import platform.test725.*

@OptIn(ExperimentalForeignApi::class)
@Composable
internal fun InteropButton() {
    Column(Modifier.fillMaxWidth().fillMaxHeight().padding(30.dp)) {
        Button(onClick = {
            println("KN: testNum call start:")
            var res = testNum(10)
            println("KN: testNum result: $res")
        }) {
            Text("Append Text")
        }

        Button(onClick = { testMemScoped() }) {
            Text("testMemScoped")
        }
    }
}

@OptIn(ExperimentalForeignApi::class)
fun testMemScoped() {
    memScoped {
        // 分配内存（包括 null 终止符）
        val buffer = allocArray<ByteVar>(5) // "abcd" + '\0'

        // 写入数据
        buffer[0] = 'a'.code.toByte()
        buffer[1] = 'b'.code.toByte()
        buffer[2] = 'c'.code.toByte()
        buffer[3] = 'd'.code.toByte()
        buffer[4] = 0 // Null 终止符

        // 传递指针给 C
        print_string(buffer)
    }
}
